<template>
  <div>
    <van-nav-bar
      :title="`${$route.query.key} 的搜索结果`"
      left-arrow
      @click-left="$router.back()"
      fixed
      placeholder
    />

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" @click="$router.push(`/article/${item.art_id}`)" :key="item.art_id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
// 导入接口
import { resultAPI } from '@/api/search'
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  methods: {
    async onLoad () {
      const res = await resultAPI({
        page: this.page,
        per_page: 10,
        q: this.$route.query.key
      })

      // 页码+1，下次来就是查下一页
      this.page++
      this.list.push(...res.data.results)
      // 把本次加载状态改为false
      this.loading = false
      // 判断是否全部加载完成
      if (this.list.length >= res.data.total_count) {
        this.finished = true
      }
    }
  }
}
</script>

<style></style>
